package discrete.math;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class GraphTheoryGUI extends JPanel implements MouseListener {

	private static MatrixMath matrix;
	private static JFrame frame = new JFrame("Matrix Math");
	private MatrixPanel panel1;
	private GraphPanel panel2;
	private PathPanel panel3;
	static JFrame jtfMainFrame;
	static JButton jbnButton1;
	static JTextField jtfInput;
	static JPanel jplPanel;

	public GraphTheoryGUI() {
		super(new GridLayout(1, 1));

		JTabbedPane tabbedPane = new JTabbedPane();
		ImageIcon icon = null;
		panel1 = makeMatrixPanel();
		tabbedPane.addMouseListener(this);
		tabbedPane.addTab("Matrix", icon, panel1, "Display the matrix page");
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

		panel2 = makeGraphPanel();

		tabbedPane.addTab("Graph", icon, panel2, "Display the graph page");
		tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

		panel3 = makePathPanel();
		// JComponent panel3 = makeTextPanel("Path Page (this page has a
		// preferred size of 410 x 50).");
		tabbedPane.addTab("Show Paths", icon, panel3, "Display the path page");
		tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

		// The following line enables to use scrolling tabs.
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

		// Add the tabbed pane to this panel.
		add(tabbedPane);
	}

	private GraphPanel makeGraphPanel() {
		GraphPanel graph = new GraphPanel(true, matrix);
		graph.setPreferredSize(new Dimension(500, 500));
		return graph;
	}

	private MatrixPanel makeMatrixPanel() {
		MatrixPanel matrixPanel = new MatrixPanel(true, matrix);
		matrixPanel.setPreferredSize(new Dimension(500, 500));
		return matrixPanel;
	}

	private PathPanel makePathPanel() {
		PathPanel pathPanel = new PathPanel(true, matrix);
		pathPanel.setPreferredSize(new Dimension(500, 500));
		return pathPanel;
	}

	protected static JComponent makeTextPanel(String text) {
		JPanel panel = new JPanel(false);
		JLabel filler = new JLabel(text);
		filler.setHorizontalAlignment(JLabel.CENTER);
		panel.setLayout(new GridLayout(1, 1));
		panel.add(filler);
		return panel;
	}

	/**
	 * Create the GUI and show it. For thread safety, this method should be
	 * invoked from the event dispatch thread.
	 * 
	 * @param matrix
	 */
	private static void createAndShowGUI() {
		jtfMainFrame = new JFrame("Discrete Math - The Matrix");
		jtfMainFrame.setSize(50, 50);
		jbnButton1 = new JButton("Create Matrix");
		jtfInput = new JTextField(5);
		JComponent textPanel = makeTextPanel("Please enter the matrix size\n(Do not exceed 20)\n");
		jbnButton1.setMnemonic(KeyEvent.VK_I); // Set ShortCut Keys
		jbnButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int input = 0;
				try {
					input = Integer.parseInt(jtfInput.getText());
					if (input <= 0 || input > 20) {
						JOptionPane.showMessageDialog(frame,
								"Please enter a number from 1 - 20");
						return;
					}
					matrix = new MatrixMath(input);
					createPanels();
				} catch (RuntimeException exception) {
					JOptionPane.showMessageDialog(frame,
							"Please enter a number from 1 - 20");
				}
			}
		});
		textPanel.setPreferredSize(new Dimension(350, 100));
		jtfMainFrame.getContentPane().add(textPanel, BorderLayout.PAGE_START);
		jtfMainFrame.add(jtfInput, BorderLayout.CENTER);
		jtfMainFrame.add(jbnButton1, BorderLayout.PAGE_END);
		jtfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jtfMainFrame.pack();
		jtfMainFrame.setVisible(true);
	}

	private static void createPanels() {

		// Create and set up the window.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(500, 400));
		frame.setMaximumSize(new Dimension(500, 400));
		frame.setMinimumSize(new Dimension(500, 400));
		frame.setResizable(false);

		// Add content to the window.
		frame.add(new GraphTheoryGUI(), BorderLayout.CENTER);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		// Schedule a job for the event dispatch thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

	public void mouseClicked(MouseEvent e) {
		if ((e.getX() > 0) && (e.getX() <= 57)) {

		} else if ((e.getX() > 57) && (e.getX() <= 114)) {
			panel2.clear();
			panel2.repaint();
		} else if ((e.getX() > 114) && (e.getX() <= 171)) {

		}
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}
